package com.libbase.activity

import android.os.Bundle
import android.os.Looper
import android.view.ViewGroup
import android.widget.ImageView
import com.alibaba.android.arouter.facade.annotation.Route
import com.base.load.DefaultLoad
import com.chan.net.Result
import com.libbase.ARouterUrl
import com.libbase.R
import com.libbase.adapter.NyqfAdapter
import com.libbase.bean.NyqfBean
import com.libbase.fragment.NewFragment
import com.libbase.glide.GlideUtil
import com.base.kotlin.mvp.BaseMvpKActivity
import com.libbase.kotlin.presenter.NyqfContract
import com.libbase.kotlin.presenter.NyqfPresenter
import com.libbase.widget.NavigationTabView
import com.squareup.haha.guava.collect.ArrayListMultimap
import kotlinx.android.synthetic.main.activity_nyqf.*
import kotlinx.android.synthetic.main.layout_cpp_buttom.*
import org.yczbj.ycvideoplayerlib.controller.VideoPlayerController
import org.yczbj.ycvideoplayerlib.manager.VideoPlayerManager
import org.yczbj.ycvideoplayerlib.player.VideoPlayer
import java.util.*

@Route(path = ARouterUrl.ACTIVITY_NYQF_MVP_KOTLIN)
class NyqfMvpKActivity : BaseMvpKActivity<NyqfPresenter>(), NyqfContract.View, NewFragment.FragmentCallBack {

    private val FIRST_INDEX = "index"
    private val SECEND_INDEX = "dangjian"
    private val THIRD_INDEX = "me"

    private var loadView: DefaultLoad? = null

    private var banners: Array<String> = arrayOf(
            "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1564396852644&di=4ab24fcef53d37ca7b6e16ec6b6f6a9b&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201409%2F19%2F20140919033302_FrnUy.jpeg",
            "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1543899454775&di=f6b657adc35d3614c474eeec42bb0dae&imgtype=0&" + "src=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F01d4c959afa1f9a8012028a9fd5f9a.jpg%401280w_1l_2o_100sh.jpg")

    private var titleListMultimap: ArrayListMultimap<String, String>? = null
    private var listMultimap: ArrayListMultimap<String, String>? = null
    private var navigationTabViewList: List<NavigationTabView>? = null
    private var pagerAdapter: NyqfAdapter? = null


    private var select = -1
    private var mTitleBarHeight: Float? = 0.toFloat()
    private var mLastScale = 1f
    private var mLastAlpha = 1f

    private var mVideoView: VideoPlayer? = null

    override fun getLayoutId(): Int {
        return R.layout.activity_nyqf
    }

    override fun useDynamicTitleBar(): Boolean {
        return false
    }

    override fun isTranslucentStatus(): Boolean {
        return true
    }

    override fun showLoading() {
        loadView?.load(fl_content)
    }

    override fun hideLoading() {
        loadView?.removeView()
    }

    override fun onError(e: Throwable) {

    }

    override fun bindViewToPresenter(): NyqfPresenter? {
        var presenter: NyqfPresenter? = NyqfPresenter()
        presenter?.attachView(this)
        return presenter
    }

    override fun initView(savedInstanceState: Bundle?) {
        loadView = DefaultLoad(mContext)

        mTitleBarHeight = resources.getDimension(R.dimen.title_bar_height) + resources.getDimension(R.dimen.dp150)
        main_abl_appbar?.setPadding(0, statusBarHeight, 0, 0)
        bannerview?.setAdapter({ _, list, position ->
            val imageView = ImageView(mContext)
            GlideUtil.loadRectangleImage(imageView, list[position], R.dimen.dp5)
            imageView
        }, banners.toList())

        titleListMultimap = ArrayListMultimap.create()
        listMultimap = ArrayListMultimap.create()

        pagerAdapter = NyqfAdapter(supportFragmentManager)

        initHeader()
        initBottom()
        initContent()
    }

    override fun release() {

    }

    override fun onSuccessNyqfTitle(result: Result<NyqfBean>) {
        // 建造底部栏和对应title的索引
        var datas = result.data
        for (navBean in datas.nav) {
            initIndex(navBean)
        }

        // 绑定我的
        val dataBean = NyqfBean.DataBean("", "我")
        titleListMultimap?.put(THIRD_INDEX, "我")
        listMultimap?.put(THIRD_INDEX, dataBean.code)

        bindDataView()
    }

    private fun initHeader() {
        mPresenter?.getTitles()
    }

    private fun initBottom() {
        rb_cpp_main?.setRadioButtonBG(R.mipmap.main_selected, R.mipmap.main_unselected, "首页");
        rb_cpp_dang?.setRadioButtonBG(R.mipmap.nyqf_selected, R.mipmap.nyqf_unselected, "反腐倡廉")
        rb_cpp_mine?.setRadioButtonBG(R.mipmap.me_selected, R.mipmap.me_unselected, "我")
        navigationTabViewList = listOf(rb_cpp_main, rb_cpp_dang, rb_cpp_mine)
    }

    private fun initContent() {
        main_vp?.adapter = pagerAdapter
    }

    private fun initIndex(navBean: NyqfBean.NavBean?) {
        if (navBean?.code.equals(FIRST_INDEX, ignoreCase = true)) { // 首页
            val dataBeans: ArrayList<NyqfBean.DataBean>? = ArrayList(navBean?.data)
            dataBeans?.addAll(listOf(NyqfBean.DataBean("live", "直播"),
                    NyqfBean.DataBean("video", "视频")))
            dataBeans?.let {
                for (dataBean in dataBeans) {
                    titleListMultimap?.put(FIRST_INDEX, dataBean.title)
                    listMultimap?.put(FIRST_INDEX, dataBean.code)
                }
            }
        } else { // 绑定其他界面
            navBean?.data?.let {
                for (dataBean in it) {
                    titleListMultimap?.put(navBean.code, dataBean.title)
                    listMultimap?.put(navBean.code, dataBean.code)
                }
            }
        }
    }

    private fun bindDataView() {
        val position = if (select == -1) 0 else select
        if (Thread.currentThread() == Looper.getMainLooper().thread) {
            hideLoading()
            select = -1
            select(position)
        }
    }

    private fun select(position: Int) {
        if (select != position) {
            navigationTabViewList?.let {
                for (tabview in it) {
                    tabview.setCheck(false)
                }
                it[position].setCheck(true)
                bindTitle(position)
            }
        }
    }

    private fun bindTitle(position: Int) {
        if (listMultimap?.size() as Int > 0) {
            var dataBeans: List<String>? = null
            var strings: List<String>? = null
            when (position) {
                0 -> {
                    dataBeans = listMultimap?.get(FIRST_INDEX)
                    strings = titleListMultimap?.get(FIRST_INDEX)
                }
                1 -> {
                    dataBeans = listMultimap?.get(SECEND_INDEX)
                    strings = titleListMultimap?.get(SECEND_INDEX)
                }
                2 -> {
                    dataBeans = listMultimap?.get(THIRD_INDEX)
                    strings = titleListMultimap?.get(THIRD_INDEX)
                }
            }
            dataBeans?.let {
                if (it.isNotEmpty()) {
                    main_vp.offscreenPageLimit = dataBeans.size
                    pagerAdapter?.setTitles(strings)
                    pagerAdapter?.setNewData(dataBeans)
                    var titles: Array<String>? = strings?.toTypedArray()
                    rcy_main.setViewPager(main_vp, titles)
                    pagerAdapter?.notifyDataSetChanged()
                }
            }
        }
    }

    /**
     * 清除videoView的父view
     */
    override fun removePlayerView() {
        if (mVideoView == null) {
            return
        }
        if (mVideoView?.parent != null) {
            val viewGroup = mVideoView?.parent as? ViewGroup
            viewGroup?.removeView(mVideoView)
        }
        mVideoView?.release()
        VideoPlayerManager.instance().releaseVideoPlayer()
    }

    override fun getVideoView(): VideoPlayer? {
        if (mVideoView == null) {
            mVideoView = VideoPlayer(mContext)
            val controller = VideoPlayerController(mContext)
            controller.setTitle("")
            mVideoView?.setController(controller)
        }
        return mVideoView
    }

    /**
     * 是否有父view
     *
     * @return
     */
    override fun hasVideoViewParent(): Boolean {
        return if (mVideoView == null) {
            false
        } else {
            mVideoView?.parent != null
        }
    }
}


